<template>
  <div class="form">
    <el-dialog v-dialogDrag class="tiny-mce-dialog" :close-on-press-escape = "true" :show-close = 'false' center :modal="false" :title="ruleForm.carOwnerId?'编辑车辆':'新增车辆'" width="600px" :visible.sync="isShow" @close="cancel" :close-on-click-modal="false">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" size="mini" :label-width="formLabelWidth">
          <el-card class="box-card">
          <el-row :gutter="24">
            <el-col :span="24">
              <el-form-item label="车牌号码" :label-width="formLabelWidth" prop="carNumber">
                <el-input v-model="ruleForm.carNumber" clearable maxlength="20" placeholder="请输入车牌号码"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="车辆类型" prop="vehicleTypeId" :label-width="formLabelWidth">
                <el-select v-model="ruleForm.vehicleTypeId" filterable placeholder="请选择" clearable @change="selectVehicleType">
                  <el-option
                      v-for="item in vehicleTypes"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="车辆长度" prop="vehicleLengthId" :label-width="formLabelWidth">
                <el-select v-model="ruleForm.vehicleLengthId" filterable placeholder="请选择" clearable @change="selectVehicleLengths" >
                  <el-option
                      v-for="item in vehicleLengths"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="车辆自重" :label-width="formLabelWidth" prop="carWight">
                <el-input-number v-model="ruleForm.carWight" :min="0" :max="999" label="车辆自重">
                  <template slot="append">吨</template>
                </el-input-number>吨
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="车辆载重" :label-width="formLabelWidth" prop="carLoad">
                <el-input-number v-model="ruleForm.carLoad" :min="0" :max="999" label="车辆载重">
                  <template slot="append">吨</template>
                </el-input-number>吨
              </el-form-item>
            </el-col>

            <el-col :span="12">
            <el-form-item label="行驶证主页" :label-width="formLabelWidth" prop="status" >
              <oss-upload ref="upload1" :limit="1" :type="'MINE_MOUTH_IMG'" :echodisplaylist="ruleForm.vehicleLicenseFirstPics" not-must="false" :custom-val="customVal" @success="update2" />
            </el-form-item>
            </el-col>
            <el-col :span="12">
            <el-form-item label="行驶证副页" :label-width="formLabelWidth" prop="status" >
              <oss-upload ref="upload3" :limit="1" :type="'MINE_MOUTH_IMG'" :echodisplaylist="ruleForm.vehicleLicenseSecondPics" not-must="false" :custom-val="customVal" @success="update3" />
            </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="车辆照片" :label-width="formLabelWidth" prop="carPhoto" >
                <oss-upload ref="upload2" :limit="1" :type="'MINE_MOUTH_IMG'" :echodisplaylist="ruleForm.carPhotos" not-must="false" :custom-val="customVal" @success="save" />
              </el-form-item>
            </el-col>
          </el-row>
          </el-card>
        </el-form>
      <div slot="footer" style="text-align:right" class="dialog-footer">
        <el-button type="primary" v-if= "this.item.falg" size="mini" v-loading.fullscreen.lock="fullscreenLoading" @click="update">保存</el-button>
        <el-button type="info" size="mini" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>


import OssUpload from '@/components/oss-upload/oss-upload.vue'
import validationRules from "@/utils/validationRules";

export default {
  name: 'DriverStaff',
  props: ['showFormPanel', 'item'],
  data () {
    return {
      vehicleTypes: [],
      vehicleLengths: [],
      matchData:{},
      showOperation:false,
      fullscreenLoading: false,
      customVal: {imgName: ''},
      showTreeDialog: false,
      selectOrgExpandAll: false,
      ruleForm: this.item,
      roleOptions: [],
      formLabelWidth: '100px',
      formLabelWidth1: '65px',
      isShow: this.showFormPanel,
      rules: {
        carNumber: [
          { required: true, message: '请输入车牌号', trigger: 'blur' }
        ],
        reserves: [
          { validator: validationRules.validateUnitNum, trigger: 'blur' }
        ],
        yearOutPut: [
          { validator: validationRules.validateUnitNum, trigger: 'blur' }
        ],
        vehicleTypeId: [
          { required: true, message: '请选择车辆类型', trigger: 'blur' }
        ],
        vehicleLengthId: [
          { required: true, message: '请选择车辆长度', trigger: 'blur' }
        ],
        carWight: [
          { required: true, message: '请输入车辆自重', trigger: 'blur' }
        ],
        carLoad: [
          { required: true, message: '请输入车辆载重', trigger: 'blur' }
        ]
      }
    }
  },
  filters: {
    formatStatus: function (val) {
      if (val === 'LOCK') {
        return false
      } else if (val === 'NORMAL') {
        return true
      }
      return '-'
    }
  },
  methods: {
    selectVehicleLengths (val) {
      if (!val){
        this.ruleForm.carLength = null
        return
      }
      let obj = {}
      obj = this.vehicleLengths.find((item) => {
        return item.id === val
      })
      this.ruleForm.carLength = obj.name
    },
    selectVehicleType (val) {
      if (!val){
        this.ruleForm.typeName = null
        return
      }
      let obj = {}
      obj = this.vehicleTypes.find((item) => {
        return item.id === val
      })
      this.ruleForm.typeName = obj.name
    },
    update(){
      let self = this;
      self.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.fullscreenLoading = true
          self.customVal.imgName = ":" + "：";
          self.$refs['upload1'].upload()
        }
      });
    },
    update2(){
      let self = this
      self.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          //self.customVal.orderNo = self.form.orderNumber +":" + self.date;
          self.$refs['upload3'].upload()
        }
      })
    },
    update3(){
      let self = this
      self.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          //self.customVal.orderNo = self.form.orderNumber +":" + self.date;
          self.$refs['upload2'].upload()
        }
      })
    },
    save () {
        let self = this
        let url = 'add'
        if (self.ruleForm.carOwnerId != null) {
          url = 'update'
        }
        var objKey = self.$refs['upload1'].uploadItem;
        var objKey1 = self.$refs['upload3'].uploadItem;
        var imgValue = self.$refs['upload1'].imgValue;
        var imgValue1 = self.$refs['upload3'].imgValue;
        var objKey2 = self.$refs['upload2'].uploadItem;
        var imgValue2 = self.$refs['upload2'].imgValue;
        if (objKey){
          objKey.forEach((value, index) => {
            if (index === 0){
              self.ruleForm.vehicleLicenseFirstPic = value.objKey  ?  imgValue + value.objKey : value.url
            }
          })
        }
        if (objKey1){
          objKey1.forEach((value, index) => {
            if (index === 0){
              self.ruleForm.vehicleLicenseSecondPic = value.objKey  ?  imgValue1 + value.objKey : value.url
            }
          })
        }
        if (objKey2) {
          objKey2.forEach((value, index) => {
            if (index === 0){
              self.ruleForm.carPhoto = value.objKey  ?  imgValue2 + value.objKey : value.url
            }
          })
        }
        self.$http.post('/api/vehicleManages/' + url, self.ruleForm)
            .then(function (res) {
              self.fullscreenLoading = false
              self.cancel()
              self.$emit('refreshList')
            }).catch(function (error) {
              self.fullscreenLoading = false
              console.log(error)
        })
    },
    search(){
      let self = this
      self.$http.get("/api/systemController/getVehicleType").then(function(value) {
        if (value.ok){
          self.vehicleLengths = value.data.vehicleLengths
          self.vehicleTypes = value.data.vehicleTypes
        }
      })
    },
    cancel (){
      this.isShow = false
      this.$emit('update:showFormPanel', this.isShow)
    }
  },
  mounted: function () {
      this.search()
  },
  components: {
    OssUpload
  }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
.el-form-item--mini.el-form-item, .el-form-item--small.el-form-item {
  margin-bottom: 15px;
}
.mark-body{
  position:fixed;
  left:0px;
  top:0px;
  background:rgba(0, 0, 0, 0.18);
  width:100%;  /*宽度设置为100%，这样才能使隐藏背景层覆盖原页面*/
  height:100%;
  filter:alpha(opacity=60);  /*设置透明度为60%*/
  opacity:0.6;  /*非IE浏览器下设置透明度为60%*/
  z-Index:999;
}
aside {
  /* background: #eef1f6; */
  /* padding: 6px 5px; */
  background: none;
  padding: 0px 24px;
  margin-bottom: 1px;
  border-radius: 2px;
  display: block;
  /* line-height: 50px; */
  /* font-size: 48px; */
  font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen,Ubuntu,Cantarell,"Fira Sans","Droid Sans","Helvetica Neue",sans-serif;
  color: #2c3e50;
  /* -webkit-font-smoothing: antialiased; */
  -moz-osx-font-smoothing: grayscale;
}
</style>
